package com.Liux.blog.service.impl;

import com.Liux.blog.dao.ArticleMapper;
import com.Liux.blog.dao.UserMapper;
import com.Liux.blog.entity.Article;
import com.Liux.blog.entity.User;
import com.Liux.blog.service.ArticleService;
import com.Liux.blog.service.UserService;
import com.Liux.blog.util.MD5Utils;
import com.Liux.blog.util.PageHelper;
import com.Liux.blog.vo.ResultStatus;
import com.Liux.blog.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public List<User> loginPwd(String name, String pwd) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("loginName",name);
        criteria.andEqualTo("password",pwd);
        List<User> users = userMapper.selectByExample(example);
        return users;
    }

    @Override
    public User getUserById(String userId) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",userId);
        List<User> users = userMapper.selectByExample(example);
        return users.get(0);
    }

    @Override
    public ResultVo addUser(User user) {
        user.setId(UUID.randomUUID().toString());
        user.setCreateTime(new Date());
        user.setPassword(MD5Utils.md5(user.getPassword()));
        int i = userMapper.insert(user);
        if(i>0){
            return new ResultVo(ResultStatus.OK,"success",user);
        }else{
            return new ResultVo(ResultStatus.NO,"fail",null);
        }
    }

    @Override
    public ResultVo listUser(int pageNum, int limit) {
        int start = (pageNum-1)*limit;
        List<User> users = userMapper.UserList(start, limit);
        //count
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        int count = userMapper.selectCountByExample(example);
        //总页数
        int pageCount = count % limit == 0 ? count / limit : count /limit + 1;

        PageHelper<User> userPageHelper = new PageHelper<>(count, pageCount, users);

        return new ResultVo(ResultStatus.OK,"success",userPageHelper);
    }

    @Override
    public ResultVo loginUser(String name, String pwd) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("loginName",name);
        pwd = MD5Utils.md5(pwd);
        criteria.andEqualTo("password",pwd);
        List<User> users = userMapper.selectByExample(example);
        if(users.size()>0){
            return new ResultVo(ResultStatus.OK,"登录成功",users);
        }else{
            return new ResultVo(ResultStatus.NO,"登录失败",null);
        }

    }

    @Override
    public ResultVo updateUser(String id, String nikeName, String signature) {
        int i = userMapper.updateUser(id, nikeName, signature);
        if(i>0){
            return new ResultVo(ResultStatus.OK,"修改成功",null);
        }else{
            return new ResultVo(ResultStatus.NO,"修改失败",null);
        }
    }

    @Override
    public ResultVo delUser(String id) {
        int i = userMapper.delUser(id);
        if(i>0){
            Example example = new Example(Article.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("userId",id);
            articleMapper.deleteByExample(example);
            return new ResultVo(ResultStatus.OK,"删除成功",null);
        }else{
            return new ResultVo(ResultStatus.NO,"删除失败",null);
        }
    }

    @Override
    public ResultVo LikeUser(String nikeName,int pageNum, int limit) {
        int start = (pageNum - 1)*limit;
        String like = "%"+nikeName+"%";
        List<User> userByLike = userMapper.getUserByLike(like,start,limit);

        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("nikeName",like);
        int count = userMapper.selectCountByExample(example);

        int pageCount = count % limit == 0?count / limit : count / limit + 1;
        PageHelper<User> userPageHelper = new PageHelper<>(count, pageCount, userByLike);
        return new ResultVo(ResultStatus.OK,"success",userPageHelper);
    }

}
